package entities;



import java.awt.Graphics;
import java.util.ArrayList;

public class Brick implements Drawable{
    private int coordenadaX;
    private int coordenadaY;
    private int ancho;
    private int alto;
    private ArrayList<CollisionListener> collisionListenerList;
    
    public Brick(int coordenadaX, int coordenadaY, int ancho, int alto){
        this.coordenadaX = coordenadaX;
        this.coordenadaY = coordenadaY;
        this.ancho = ancho;
        this.alto = alto;
        collisionListenerList = new ArrayList<>();
    }

    @Override
    public void render(Graphics g) {
        g.fillRect(coordenadaX, coordenadaY, ancho, alto);
    }

    @Override
    public void add(Drawable d) {

    }

    @Override
    public void remove(Drawable d) {
        
    }

    @Override
    public Drawable getChild(int i) {
        return null;
    }

    @Override
    public Drawable colision(Drawable d) {
        Ball ball = (Ball) d;
        if (coordenadaX - 2 <= ball.getCoordenadaX() + ball.getRadio() && ball.getCoordenadaX() + ball.getRadio() <= coordenadaX + ancho + 2){
            if (coordenadaY + alto <= ball.getCoordenadaY() && ball.getCoordenadaY() <= coordenadaY + alto + 2){
                ball.setVelocidadY(ball.getVelocidadY() * -1);
            }
            if (coordenadaY <= ball.getCoordenadaY() + ball.getRadio() * 2 && ball.getCoordenadaY() + ball.getRadio() * 2 <= coordenadaY + 2){
                ball.setVelocidadY(ball.getVelocidadY() * -1);
            }
        }
        if (coordenadaY <= ball.getCoordenadaY() && ball.getCoordenadaY() <= coordenadaY + alto){
            if (coordenadaX - 2 <= ball.getCoordenadaX() + 2 * ball.getRadio() && ball.getCoordenadaX() + 2 * ball.getRadio() <= coordenadaX){
                ball.setVelocidadX(ball.getVelocidadX() * -1);
            }
            if (coordenadaX + ancho <= ball.getCoordenadaX() && ball.getCoordenadaX() <= coordenadaX + ancho + 2){
                ball.setVelocidadX(ball.getVelocidadX() * -1);
            }
        }
        return null;
    }
    
    public void addCollisionListener(CollisionListener collisionListener){
        collisionListenerList.add(collisionListener);
    }
    
    private void collision(){
        for (CollisionListener l : collisionListenerList){
            l.onCollision(1);
        }
    }
}
